package com.xiaode.controller.admin.authority;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.redis.Redis;
import com.xiaode.common.anotation.Authority;
import com.xiaode.common.anotation.JSONArrayString;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;

import javax.validation.constraints.*;
import java.util.List;
import java.util.Set;

@Path("/authority")
@Api("权限配置相关服务")
@EnableCORS
public class AuthorityController extends Controller {

    @Inject
    private AuthorityService service;

    /**
     * 列表
     */
    @ApiOper("角色列表")
    @Authority(description = "角色列表", permission = "admin:authority:list", permissionParentName = "管理员模块", permissionName = "角色管理", type = 1)
    public void list(
            @ApiPara(value = "state", notes = "状态值，多个','隔开") String state,
            @ApiPara(value = "name", notes = "名称") String name) throws Exception {
        Ret ret = service.list(name, state);
        renderJson(ret);
    }

    /**
     * 添加
     */
    @ApiOper("角色添加")
    @Authority(description = "角色添加", permission = "admin:authority:create", permissionParentName = "管理员模块", permissionName = "角色管理", type = 2)
    public void create(@ApiPara(value = "desc", notes = "说明") String desc,
                       @NotBlank @ApiPara(value = "name", notes = "名称") String name
                      ) throws Exception {
        Ret ret = service.create(name, desc);
        renderJson(ret);
    }

    /**
     * 删除
     */
    @ApiOper("删除角色")
    @Authority(description = "删除角色", permission = "admin:authority:delete", permissionParentName = "管理员模块", permissionName = "角色管理", type = 2)
    public void delete(@Size @ApiPara(value = "roleId", notes = "角色Id") Integer roleId) throws Exception {
        Ret ret = service.delete(roleId);
        renderJson(ret);
    }

    /**
     * 更新
     */
    @ApiOper("修改角色")
    @Authority(description = "修改角色", permission = "admin:authority:update", permissionParentName = "管理员模块", permissionName = "角色管理", type = 2)
    public void update(@Size @ApiPara(value = "id", notes = "角色Id") Integer id,
                       @ApiPara(value = "name", notes = "名称") String name,
                       @ApiPara(value = "desc", notes = "说明") String desc,
                       @ApiPara(value = "state", notes = "状态") Integer state,
                       @ApiPara(value = "readOnly", notes = "只读/编辑 1.只读 2.编辑") Integer readOnly) throws Exception {
        Ret ret = service.update(id, name, desc, state, readOnly);
        renderJson(ret);
    }

    /**
     * 角色批量授权
     */
    @ApiOper("角色批量授权")
    @Authority(description = "角色授权", permission = "admin:authority:setPermission", permissionParentName = "管理员模块", permissionName = "角色管理", type = 2)
    public void setPermission(@JSONArrayString @ApiPara(value = "trueList", notes = "增加的角色Id[id]") String trueList,
                              @JSONArrayString @ApiPara(value = "falseList", notes = "删除角色的id列表") String falseList,
                              @NotBlank @ApiPara(value = "authorityId", notes = "权限id") Integer authorityId) throws Exception {
        Ret ret = service.setPermission(trueList, falseList, authorityId);
        renderJson(ret);
    }

    /**
     * 角色授权
     */
    @ApiOper("角色授权")
    @Authority(description = "角色授权", permission = "admin:authority:setRolePermission", permissionParentName = "管理员模块", permissionName = "角色管理", type = 2)
    public void setRolePermission(@Size @ApiPara(value = "roleId", notes = "角色id") Integer roleId,
                                  @Size @ApiPara(value = "readOnly", notes = "是否只读") Integer readOnly,
                                  @JSONArrayString @ApiPara(value = "permissionIds", notes = "[permissionId]") String permissionIds) throws Exception {
        Ret ret = service.setRolePermission(roleId, permissionIds, readOnly);
        renderJson(ret);
    }

    /**
     * 权限列表
     */
    @ApiOper("角色权限列表")
    @Authority(description = "角色权限列表", permission = "admin:authority:permissionList", permissionParentName = "管理员模块", permissionName = "角色管理", type = 1)
    public void permissionList(@Size @ApiPara(value = "roleId", notes = "角色Id") Integer roleId) throws Exception {
        List<Kv> permPointTree = Redis.use().get("permPointTree");
        Set<String> allPermPoint = Redis.use().get("allPermPoint");
        Ret ret = service.permissionList(roleId, permPointTree, allPermPoint);
        renderJson(ret);
    }


    /**
     * 角色赋值给教师
     */
    @ApiOper("角色赋值给教师")
    @Authority(description = "角色赋值给教师", permission = "admin:authority:roleToTeacher", permissionParentName = "管理员模块", permissionName = "权限管理", type = 2)
    public void roleToTeacher(@JSONArrayString @ApiPara(value = "trueList", notes = "角色添加列表[id]") String trueList,
                              @JSONArrayString @ApiPara(value = "falseList", notes = "角色删除列表[id]") String falseList,
                              @Size @ApiPara(value = "roleId", notes = "roleId") Integer roleId) throws Exception {
        Ret ret = service.roleToTeacher(trueList, falseList, roleId);
        renderJson(ret);
    }

    /**
     * 查询拥有角色教师id
     */
    @ApiOper("查询拥有角色教师id")
    @Authority(description = "拥有角色成员id", permission = "admin:authority:roleMember", permissionParentName = "管理员模块", permissionName = "角色管理", type = 1)
    public void roleMemberIds(@Size @ApiPara(value = "roleId", notes = "角色id") Integer roleId) throws Exception {
        Ret ret = service.roleMemberIds(roleId);
        renderJson(ret);
    }


    /**
     * 查询所有权限
     */
    @ApiOper("查询权限")
    @Authority(description = "查询权限", permission = "admin:authority:findAuthority", permissionParentName = "管理员模块", permissionName = "权限管理", type = 1)
    public void findAuthority() throws Exception {
        Ret ret = service.findAuthority();
        renderJson(ret);
    }

    /**
     * 查询角色拥有的权限
     */
    @ApiOper("查询角色拥有的权限")
    @Authority(description = "查询角色拥有的权限", permission = "admin:authority:findRolePermission", permissionParentName = "管理员模块", permissionName = "权限管理", type = 1)
    public void findRolePermission(@Size @ApiPara(value = "roleId", notes = "角色Id") Integer roleId) throws Exception {
        Ret ret = service.findRolePermission(roleId);
        renderJson(ret);
    }

    /**
     * 查询拥有权限的角色
     */
    @ApiOper("查询拥有权限的角色")
    @Authority(description = "查询角色拥有的权限", permission = "admin:authority:findPermissionRole", permissionParentName = "管理员模块", permissionName = "权限管理", type = 1)
    public void findPermissionRole(@Size @ApiPara(value = "authorityId", notes = "权限id") Integer authorityId) throws Exception {
        Ret ret = service.findPermissionRole(authorityId);
        renderJson(ret);
    }

    /**
     * 编辑权限
     */
    @ApiOper("编辑权限")
    @Authority(description = "编辑权限", permission = "admin:authority:editAuthority", permissionParentName = "管理员模块", permissionName = "权限管理", type = 2)
    public void editAuthority(@Size @ApiPara(value = "authorityId", notes = "权限id") Integer authorityId,
                              @ApiPara(value = "describe", notes = "描述") String describe) throws Exception {
        Ret ret = service.editAuthority(authorityId, describe);
        renderJson(ret);
    }


    /**
     * 添加节点
     */
    @ApiOper("添加节点")
    @Authority(description = "添加节点", permission = "admin:authority:addNode", permissionParentName = "管理员模块", permissionName = "架构管理", type = 2)
    public void addNode(@Size(min = 1,max = 3) @ApiPara(value = "stratum", notes = "节点层级 1.根节点 2.年级 3.班级") Integer stratum,
                        @NotEmpty @ApiPara(value = "name", notes = "节点名称") String name,
                        @ApiPara(value = "parentNodeId", notes = "父节点Id") Integer parentNodeId) throws Exception {

        Ret ret = service.addNode(stratum, name, parentNodeId);
        renderJson(ret);
    }

    /**
     * 删除节点
     */
    @ApiOper("删除节点")
    @Authority(description = "删除节点", permission = "admin:authority:delNode", permissionParentName = "管理员模块", permissionName = "架构管理", type = 2)
    public void delNode(@Size(min = 1,max = 3) @ApiPara(value = "stratum", notes = "节点层级 1.根节点 2.年级 3.班级") Integer stratum,
                        @NotEmpty @ApiPara(value = "nodeId",notes = "节点Id")Integer nodeId)throws Exception{
        Ret ret = service.delNode(stratum,nodeId);
        renderJson(ret);
    }

    /**
     * 修改节点
     */
    @ApiOper("修改节点")
    @Authority(description = "修改节点", permission = "admin:authority:updateNode", permissionParentName = "管理员模块", permissionName = "架构管理", type = 2)
    public void updateNode(@Min(1) @Max(3) @ApiPara(value = "stratum", notes = "节点层级 1.根节点 2.年级 3.班级") Integer stratum,
                           @NotEmpty @ApiPara(value = "nodeId",notes = "节点Id")Integer nodeId,
                           @NotBlank @ApiPara(value = "name",notes = "新的描述")String name)throws Exception{
        Ret ret = service.updateNode(stratum,nodeId,name);
        renderJson(ret);
    }

    /**
     * 架构查询
     */
    @ApiOper("查询架构管理")
    @Authority(description = "查询架构管理", permission = "admin:authority:findNode", permissionParentName = "管理员模块", permissionName = "架构管理", type = 1)
    public void findNode()throws Exception{
        Ret ret = service.findNode();
        renderJson(ret);
    }

    /**
     * 成员管理
     */
    @ApiOper("节点成员管理")
    @Authority(description = "节点成员管理", permission = "admin:authority:nodeMemberManage", permissionParentName = "管理员模块", permissionName = "架构管理", type = 2)
    public void nodeMemberManage(@Min(1) @Max(3) @ApiPara(value = "stratum", notes = "节点层级 1.根节点 2.年级 3.班级") Integer stratum,
                                 @NotEmpty @ApiPara(value = "nodeId",notes = "节点Id")Integer nodeId,
                                 @NotBlank @ApiPara(value = "idList",notes = "id列表")String idList)throws Exception{
        Ret ret = service.nodeMemberManage(stratum,nodeId,idList);
        renderJson(ret);
    }
}